package com.yykj.order.center.server.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.pagehelper.Page;
import com.yykj.order.center.client.dto.ConsultationDoctorListDto;
import com.yykj.order.center.client.form.consultation.ConsultationAdminDoctorListForm;
import com.yykj.order.center.server.entity.Consultation;
import com.yykj.order.center.server.entity.ConsultationDoctor;
import com.yykj.order.center.server.util.StringUtil;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ConsultationDoctorMapper extends BaseMapper<ConsultationDoctor> {

    default List<ConsultationDoctor> findByConsultationId(String consultationId) {
        LambdaQueryWrapper<ConsultationDoctor> consultationDoctorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        consultationDoctorLambdaQueryWrapper.eq(ConsultationDoctor::getConsultationId, consultationId);
        consultationDoctorLambdaQueryWrapper.orderByDesc(ConsultationDoctor::getSort);
        consultationDoctorLambdaQueryWrapper.orderByDesc(ConsultationDoctor::getAdviseModifytime);
        return this.selectList(consultationDoctorLambdaQueryWrapper);
    }

    default ConsultationDoctor findByConsultationIdAndDoctorId(String consultationId, String removeDoctorId) {
        LambdaQueryWrapper<ConsultationDoctor> consultationDoctorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        consultationDoctorLambdaQueryWrapper.eq(ConsultationDoctor::getConsultationId, consultationId);
        consultationDoctorLambdaQueryWrapper.eq(ConsultationDoctor::getDoctorId, removeDoctorId);
        return this.selectOne(consultationDoctorLambdaQueryWrapper);
    }

    @SelectProvider(type = ConsultationMapperProvider.class, method = "findList")
    Page<ConsultationDoctorListDto> findList(@Param("doctorId") String doctorId, @Param("hospitalId") String hospitalId, @Param("status") Integer status);

    @Select({ "select max(sort) from core_consultation_doctor where consultation_id = #{consultationId} " })
    int findMaxSortByConsultationId(String consultationId);

    class ConsultationMapperProvider {
        public String findList(@Param("doctorId") String doctorId, @Param("hospitalId") String hospitalId, @Param("status") Integer status) {
            SQL sql = new SQL();
            sql.SELECT("a.*,b.patient_name, b.patient_gender, b.patient_age,b.patient_medical_no,b.type_id,b.type_name,b.apply_doctor_name")
                    .SELECT("b.apply_doctor_hospital_name,b.apply_doctor_department_name,b.order_no")
                    .FROM(" core_consultation_doctor a LEFT JOIN core_consultation b on a.consultation_id = b.id");
            if (StringUtil.isNotBlank(doctorId)) {
                sql.WHERE(" a.doctor_id = #{doctorId}");
            }
            if (StringUtil.isNotBlank(hospitalId)) {
                sql.WHERE(" a.doctor_hospital_id = #{hospitalId}");
            }
            if (status != null) {
                sql.WHERE(" a.status = #{status}");
            }
            sql.ORDER_BY("addtime desc");
            return sql.toString();
        }
    }
}
